/*!
//      
//      \file	k2k_StateHandlerRdy2.c
//      
//      \author	Copyright 2010 Valentina Vuksic <vuksv1@bfh.ch>, Beat Liechti <liecb2@bfh.ch>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
//
//      Abstract Data Type for the state handler of the state "Rdy2"
//      
//      \brief		CAS-EBX-03 Projektarbeit 
//      			Valentina Vuksic (VUKSV1), Beat Liechti (LIECB2)
//      			StateHandlerRdy2 ADT for the k2k (Kaffee2000) application
//      \version	1.0
//      \date		2010-04-11
//      \remark		Last Modifications:
//      \remark  	v1.0, VUKSV1/LIECB2, 2010-04-11 Initial release
//
*/

#include <unistd.h>
#include <stdio.h>

#include "k2k_StateHandlerRdy2.h"
#include "k2k_AudioPlayer.h"

#define STATE_TEXT	"Choose drink - preparing two, press S4 for one"

/* -- static function declarations -----------------------------------*/

static k2k_State k2k_StateHandlerRdy2_Handler(k2k_Event event, k2k_State previousState, k2k_StateHandlerData *handlerData);

/* -- ADT functions --------------------------------------------------*/

/*!
//
// Creates StateHandlerRdy2 ADT "object"
//
// \param stateMachine is the pointer to the StateMachine structure
// 
// \return pointer to the StateHandlerRdy2 ADT
//
*/
k2k_StateHandlerRdy2* k2k_StateHandlerRdy2_Create(k2k_StateMachine *stateMachine)
{
	k2k_StateHandlerRdy2 *stateHandlerRdy2 = NULL;
	stateHandlerRdy2 = malloc(sizeof(k2k_StateHandlerRdy2));
	if (stateHandlerRdy2 != NULL)
	{
		/*! initialize structure */		
		stateHandlerRdy2->handler = k2k_StateHandlerRdy2_Handler;
		/*! create state handler data */
		stateHandlerRdy2->handlerData = k2k_StateHandlerData_Create(stateHandlerRdy2);
		if (stateHandlerRdy2->handlerData != NULL)
		{
			/*! add the state handler to the state machine */
			k2k_StateMachine_AddStateHandler(stateMachine, STATE_RDY2, stateHandlerRdy2->handler, stateHandlerRdy2->handlerData);
			/*! store reference to state machine */
			stateHandlerRdy2->stateMachine = stateMachine;
		}
	}
	return stateHandlerRdy2;
}

/*!
//
// Releases StateHandlerOff ADT "object"
// 
// \param stateHandlerRdy2 is the pointer to the StateHandlerRdy2 structure 
// 
// \return nothing
//
*/
void k2k_StateHandlerRdy2_Release(k2k_StateHandlerRdy2 *stateHandlerRdy2)
{
	if (stateHandlerRdy2 != NULL)
	{		
		if (stateHandlerRdy2->handlerData != NULL)
		{
			/*! release handler data */
			k2k_StateHandlerData_Release(stateHandlerRdy2->handlerData);
			stateHandlerRdy2->handlerData = NULL;
		}
		/*! release memory */		
		free(stateHandlerRdy2);	
	}
}

/* -- static functions -----------------------------------------------*/

/*!
//
// Actual state handler for the state "Rdy2"
//
// \param event describes the event that happened
// \param customData points to custom data (typically the corresponding ADT struct)
//
// \return next state of the event machine
//
*/
static k2k_State k2k_StateHandlerRdy2_Handler(k2k_Event event, k2k_State previousState, k2k_StateHandlerData *handlerData)
{
	/*! get the state handler structure */
	k2k_StateHandlerRdy2 *stateHandlerRdy2 = handlerData->customData;
	
	/*! check if entering the state */
	if (previousState != STATE_RDY2)
	{
		/*! set state text */
		k2k_NanoX_ShowStateText(stateHandlerRdy2->stateMachine->nanoX, STATE_TEXT);
		/*! show two cups */
		k2k_NanoX_DrawCups(stateHandlerRdy2->stateMachine->nanoX, WHITE, WHITE);
		
		/*! play rdy clip once, if the user changed to make 1 cup */
		if (previousState == STATE_RDY1)
		{
			k2k_AudioPlayNoSignal(&stateHandlerRdy2->pid, CLIP_RDY2);										
		}		
	}	

	/*! the default is to remain in the same state */	
	int nextState = STATE_RDY2;
	switch (event)
	{
		case EVENT_S0:
			/*! go into "Off" state */		
			nextState = STATE_OFF;
			break;
		case EVENT_S1: 
			nextState = STATE_RSTR;
			break;
		case EVENT_S2:
			/*! go into "Espr" state */
			nextState = STATE_ESPR;
			break;
		case EVENT_S3: 
			/*! go into "MlkC" state */
			nextState = STATE_MLKC;
			break;
		case EVENT_S4: 
			/*! go into "Rdy1" state */
			nextState = STATE_RDY1;
			break;
		case EVENT_S6: 
			/*! go into "Cln" state */
			nextState = STATE_CLN;
			break;			
		case EVENT_TIMER: 	
			/*! stay in "Rdy2" state */
			break;					
		default: 
			break;
	}	
	return nextState;
}



